package com.warrior.main;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.androidWarrior.R;
import com.warrior.bluetooth.CommHandler;
import com.warrior.bluetooth.MyMessage;
import com.warrior.bluetooth.Sync;
import com.warrior.games.Game;
import com.warrior.games.activitys.SpeedCalculationGameActivity;
import com.warrior.games.activitys.SpeedClickGameActivity;
import com.warrior.games.activitys.SpeedTestPhoneGameActivity;
import com.warrior.main.MyApp.APP_STATES;
import com.warrior.main.MyApp.ISelectGame;

public class SelectGameActivity extends MyActivity 
	implements OnClickListener, ISelectGame{
	
	private Button butSpeedTestPhone,butSpeedClick,butSpeedCalculation;
	private TextView tvMessage;
	private boolean competitorDeviceIsReady,isMoveToGameActivity;
	private MyApp app;
	public static final long SEND_DEVICE_READY = 200,RECEVIE_DEVICE_READY = 200;
	public static final long SEND_DEVICE_NOT_READY = 201,RECEVIE_DEVICE_NOT_READY = 201;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_game);
		app = (MyApp)getApplication();
		competitorDeviceIsReady = false;
		isMoveToGameActivity = false;
		buildViewReference();
		buildViewEvent();
		app.setDataReceiveSelectGameListener(this);
		Log.d(MyLog.SELECT_GAME_ACTIVITY,"into on create");
	}
	private void buildViewReference(){
		butSpeedTestPhone = (Button)findViewById(R.id.butSpeedTestPhone);
		butSpeedClick = (Button)findViewById(R.id.butSpeedClick);
		butSpeedCalculation = (Button)findViewById(R.id.butSpeedCalculation);
		tvMessage =  (TextView)findViewById(R.id.tvMessage);
	}
	private void buildViewEvent(){
		butSpeedTestPhone.setOnClickListener(this);
		butSpeedCalculation.setOnClickListener(this);
		butSpeedClick.setOnClickListener(this);
	}
	public void onClick(View v) {
		//server side
		long gameSelected = 0;
		switch(v.getId()){
			case R.id.butSpeedTestPhone:{
				gameSelected = Game.SPEED_TEST_PHONE;
				break;
			}
			case R.id.butSpeedClick:{
				gameSelected = Game.SPEED_CLICK;
				break;
			}
			case R.id.butSpeedCalculation:{
				gameSelected = Game.SPEED_CALCULATION;
				break;
			}
		}
		CommHandler commHandler = app.getCommHandler();
		commHandler.writeToRmoteDevice(gameSelected);
		Sync.waitTime(app.getTimeAir());
		NextToGameActivity(gameSelected);
	}
	protected void onResume() {
		super.onResume();
		app.setState(APP_STATES.SELECT_GAME);
		if(app.isConnectedWithOtherDevice()){
			app.getCommHandler().writeToRmoteDeviceWithCallBack(SEND_DEVICE_READY);
		}
	}
	@Override
	protected void onPause() {
		if(isMoveToGameActivity){
			isMoveToGameActivity = false;
			super.onPause();
			return;
		}
		if(app.isConnectedWithOtherDevice()){
			app.getCommHandler().writeToRmoteDevice(SEND_DEVICE_NOT_READY);
		}
		super.onPause();
	}
	
	public void onBackPressed() {
		super.onBackPressed();
		app.closeConnection();
	}
	public void dataReceiveSelectGame(Long data) {
		Log.d(MyLog.SELECT_GAME_ACTIVITY,"into dataReceiveSelectGame");
		if(data == RECEVIE_DEVICE_NOT_READY){
			competitorDeviceIsReady = false;
			setButtonWhenOtherDeviceReady(competitorDeviceIsReady);
			return;
		}
		else if(data == RECEVIE_DEVICE_READY){
			competitorDeviceIsReady = true;
			setButtonWhenOtherDeviceReady(competitorDeviceIsReady);
			return;
		}
		NextToGameActivity(data);
	}
	private void setButtonWhenOtherDeviceReady(boolean isReady){
		// just server can choose a game
		if(app.isServer()){
			butSpeedTestPhone.setEnabled(isReady);
			butSpeedClick.setEnabled(isReady);
			butSpeedCalculation.setEnabled(isReady);
			if(!isReady){
				tvMessage.setText("your partner is not ready to start game");
			}
			else{
				tvMessage.setText(getResources().getString(R.string.select_game));
			}
		}
	}
	private void NextToGameActivity(long data){
		
		if(data == Game.SPEED_TEST_PHONE){
			isMoveToGameActivity = true;
			Intent i = new Intent(this
					,SpeedTestPhoneGameActivity.class);
			startActivity(i);
		}
		else if(data == Game.SPEED_CLICK){
			isMoveToGameActivity = true;
			Intent i = new Intent(this
					,SpeedClickGameActivity.class);
			startActivity(i);
		}
		else if(data == Game.SPEED_CALCULATION){
			isMoveToGameActivity = true;
			Intent i = new Intent(this
					,SpeedCalculationGameActivity.class);
			startActivity(i);
		}
	}
}
